package com.thientan.service

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook

import com.thientan.export.ExportRequest
import com.thientan.model.HopDong
import com.thientan.utils.Constants

class PhieuThuService extends AbstractService {
	/**
	 * Return the widths of columns in the export file when export list of domains.
	 */
	protected Long[] getColWidth() {
		return [200, 200, 200] as Long[]
	}

	/**
	 * Return the column's headers in the export file when export list of domains.
	 */
	protected String[] getColTitles() {
		return ['Số tiền', 'Tên nhân viên', 'Loại chi tiết'] as String[]
	}

	/**
	 * Return the fields of domain to export.
	 */
	protected String[] getColNames() {
		return ['soTien', 'nhanVien-hoTen', 'loaiChiTiet'] as String[]
	}

	/**
	 * Return the type of columns when export list of domains.
	 */
	@SuppressWarnings("rawtypes")
	protected Class<?>[] getExportColType() {
		return [String.class, String.class, String.class] as Class<?>[]
	}

	void prepareParametersBeforeExport(ExportRequest er){
		er.addParameter("maHd", this.exportData?.maHd)
	}

	Map processImport(File importFile, String extension) {
		Sheet sheet = null
		InputStream myxls = new FileInputStream(importFile)
		if (extension == Constants.XLS_FORMAT) {
			sheet = new HSSFWorkbook(myxls).getSheetAt(0)
		} else {
			sheet = new XSSFWorkbook(myxls).getSheetAt(0)
		}

		List emptyLines = []
		List emptyMaHds = []
		List emptySoTiens = []
		List invalidSoTiens = []
		List exceedAmountHds = []
		Map hdInfo = [:]
		for (int i = 1; i <= sheet.physicalNumberOfRows; i++) {
			Row row = sheet.getRow(i)
			if (!row) {
				emptyLines << i
			} else {
				Cell maHdCell = row.getCell(0)
				Cell soTienCell = row.getCell(1)
				if (maHdCell == null || maHdCell.stringCellValue == null || maHdCell.stringCellValue == "") {
					emptyMaHds << i
					continue
				}
				if (!soTienCell) {
					emptySoTiens << i
					continue
				}
				Double soTien
				if (soTienCell.cellType == Cell.CELL_TYPE_STRING) {
					try {
						soTien = Double.valueOf(soTienCell.stringCellValue)
					} catch (Exception e) {
						invalidSoTiens << i
						continue
					}
				} else if (soTienCell.cellType == Cell.CELL_TYPE_NUMERIC) {
					soTien = soTienCell.numericCellValue
				}
				hdInfo.put(maHdCell.stringCellValue, soTien)
			}
		}

		List<HopDong> updatedHds = HopDong.findAllByMaHdInList(hdInfo.keySet())
		long numOfUpdate = 0
		updatedHds.each {
			println it.maHd
			int result = it.updateTraGop(hdInfo.get(it.maHd))
			if (result == 1) {
				exceedAmountHds << it.maHd
			} else {
				++numOfUpdate
				it.save()
			}
		}

		return ["updated" : numOfUpdate, 
				"emptyLines" : emptyLines, 
				"emptyMaHds" : emptyMaHds, 
				"emptySoTiens" : emptySoTiens, 
				"exceedAmountHds" : exceedAmountHds,
				"invalidSoTiens" : invalidSoTiens]
	}
}
